#include "Button.h"
#include <iostream>

Button::Button(float x, float y, float w, float h)
: m_x(x), m_y(y), m_width(w), m_height(h), m_hovering(false), m_clicked(false), m_mouseX(0), m_mouseY(0)
{
    setImage("button.png");
    setClickedImage("clickedButton.png");

    m_image.SetPosition(x, y);
    m_clickedImage.SetPosition(x, y);
}

void Button::resize(float w, float h)
{
    m_width = w;
    m_height = h;
}

void Button::setPosition(float x, float y)
{
    m_x = x;
    m_y = y;
    m_image.SetPosition(x, y);
    m_clickedImage.SetPosition(x, y);
}

void Button::render(GameManager* mgr)
{
    //handleEvents(mgr);
    if(clicked())
        mgr->draw(m_clickedImage);
    else
        mgr->draw(m_image);
}

bool Button::clicked() { return m_clicked; }
bool Button::hovering() { return m_hovering; }

void Button::handleEvents(GameManager* mgr)
{
    //mgr->getEvent();
    m_mouseX = mgr->getWindow().GetInput().GetMouseX();
    m_mouseY = mgr->getWindow().GetInput().GetMouseY();


    if(m_mouseX >= m_x && m_mouseX <= m_x + m_width &&
       m_mouseY >= m_y && m_mouseY <= m_y + m_height)
    {
        m_hovering = true;
        if(mgr->getWindow().GetInput().IsMouseButtonDown(sf::Mouse::Left))
        {
            m_clicked = true;
        }
        else
        {
            m_clicked = false;
        }
    }
    else
    {
        m_hovering = false;
        m_clicked = false;
    }
}

void Button::setImage(std::string name)
{
    m_image.SetImage(ImageManager::getInstance()->getImage(name));
}

void Button::setClickedImage(std::string name)
{
    m_clickedImage.SetImage(ImageManager::getInstance()->getImage(name));
}
